﻿using System;
using System.Data;
using System.Collections.Generic;
using Maticsoft.Common;
using jnagr.Model;
namespace jnagr.BLL
{
    /// <summary>
    /// devinfo
    /// </summary>
    public partial class devinfo
    {
        private readonly jnagr.DAL.devinfo dal = new jnagr.DAL.devinfo();
        public devinfo()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return dal.GetMaxId();
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int KeyId)
        {
            return dal.Exists(KeyId);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(jnagr.Model.devinfo model)
        {
            return dal.Add(model);
        }
        public string getYBQ(string devid, string type)
        {
            string s = "";
            jnagr.Model.devinfo d = dal.GetModel(devid, type);
            if (d != null)
            {
                s = d.mmark;
                d = dal.GetModel(d.mmark);
                if (d != null)
                {
                    if (!string.IsNullOrEmpty(d.chn0.Trim())) s += "," + d.chn0;
                    if (!string.IsNullOrEmpty(d.chn1.Trim())) s += "," + d.chn1;
                    if (!string.IsNullOrEmpty(d.chn2.Trim())) s += "," + d.chn2;
                    if (!string.IsNullOrEmpty(d.chn3.Trim())) s += "," + d.chn3;
                }
            }
            if (s == "") s = ",";
            return s;
        }
        public bool Updateyx(jnagr.Model.devinfo model)
        {
            return dal.Updateyx(model);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(jnagr.Model.devinfo model)
        {
            return dal.Update(model);
        }
        public bool Updatecs(jnagr.Model.devinfo model)
        {
            return dal.Updatecs(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int KeyId)
        {

            return dal.Delete(KeyId);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string KeyIdlist)
        {
            return dal.DeleteList(KeyIdlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public jnagr.Model.devinfo GetModel(int KeyId)
        {

            return dal.GetModel(KeyId);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public jnagr.Model.devinfo GetModelByCache(int KeyId)
        {

            string CacheKey = "devinfoModel-" + KeyId;
            object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(KeyId);
                    if (objModel != null)
                    {
                        int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
                        Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (jnagr.Model.devinfo)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return dal.GetList(Top, strWhere, filedOrder);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<jnagr.Model.devinfo> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<jnagr.Model.devinfo> DataTableToList(DataTable dt)
        {
            List<jnagr.Model.devinfo> modelList = new List<jnagr.Model.devinfo>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                jnagr.Model.devinfo model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = dal.DataRowToModel(dt.Rows[n]);
                    if (model != null)
                    {
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            return dal.GetRecordCount(strWhere);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  BasicMethod
        #region  ExtensionMethod
        public void Addcbd(string devid, DateTime sdt, string time, string tadd)
        {
            DateTime? mdt = dal.getMaxcbdtime(devid);
            if (mdt == null)
            {
                mdt = sdt;
            }
            string[] hms = time.Split(':');
            int h = 6;
            int m = 30;
            int s = 30;
            int th = 1;
            int.TryParse(tadd, out th);
            if (hms.Length > 0)
                int.TryParse(hms[0], out h);
            if (hms.Length > 1)
                int.TryParse(hms[1], out m);
            if (hms.Length > 2)
                int.TryParse(hms[2], out s);
            DateTime bdt = new DateTime(mdt.Value.Year, mdt.Value.Month, mdt.Value.Day, h, m, s);
            bdt = bdt.AddHours(th);
            while (bdt < DateTime.Now)
            {
                dal.Addcbd(devid, bdt);
                bdt = bdt.AddHours(th);
            }
        }
        public void Addybq(string devid, DateTime sdt, int time)
        {
            DateTime? mdt = dal.getMaxybqtime(devid);
            if (mdt == null)
            {
                mdt = sdt;
            }
            int h = 7;
            int m = 25;
            int s = 36;
            DateTime bdt = new DateTime(mdt.Value.Year, mdt.Value.Month, mdt.Value.Day, h, m, s);
            bdt = bdt.AddHours(time);
            while (bdt < DateTime.Now)
            {
                dal.Addybq(devid, bdt);
                bdt = bdt.AddHours(time);
            }
        }
        public void Addxqh(string devidm, string devids, DateTime dt)
        {
            dal.Addxqh(devidm, devids, dt);
        }
        #endregion  ExtensionMethod
    }
}

